package com.dq.Week3.day12.list;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class ArtShowUtils {
    public static void main(String[] args) throws FileNotFoundException {
        List<ArtShow> list = ArtShowUtils.creatList();
        for (ArtShow artShow : list) {
            System.out.println(artShow);
        }
        System.out.println("===================");
        ArtShow artShow = ArtShowUtils.getMaxCount(list);
        System.out.println(artShow);
        ArtShowUtils.getShowedNames(list);

    }





    public static List<String> getShowedNames(List<ArtShow> data)
    {
        ArrayList<String> result = new ArrayList<>();
        for (ArtShow artShow : data) {
            if(artShow.getStatus().equals(1))
            {
                result.add(artShow.getStuName());
            }
        }

        return result;
    }
    public static ArtShow getMaxCount(List<ArtShow> data)
    {
        data.sort((a,b) ->b.getCount() - a.getCount());
        return data.get(0);
    }
    public static void sortArtListByCount(List<ArtShow> data){
        data.sort( (a,b)->b.getCount()-a.getCount() );
    }

    public static List<String> getStuNames(List<ArtShow> data)
    {
        ArrayList<String> result = new ArrayList<>();
        //从data中获取学生的名字，放入result中；
        Iterator<ArtShow> iterator = data.iterator();
        while (iterator.hasNext()) {
            ArtShow artShow = iterator.next();
            String stuName = artShow.getStuName();
            result.add(stuName);
        }
        return result;
    }

    public static List<ArtShow> creatList() throws FileNotFoundException {
        ArrayList<ArtShow> artShows = new ArrayList<>();


//        ArtShow artShow = new ArtShow();
//        artShow.setStuName("孙博");
//        artShow.setShowName("影子");
//        artShow.setStatus(1);
//        artShow.setCount(1);

        //Scanner的用法
        String filepath = "D:\\artshow.txt";
        File file = new File(filepath);
        Scanner scanner = new Scanner(file);
        while (scanner.hasNext())
        {
            String line = scanner.nextLine();

            ArtShow artShow = createArtShow(line);
            artShows.add(artShow);
        }
        scanner.close();

        return artShows;
    }

    private static ArtShow createArtShow(String line) {
        String s = "\t";
        String[] split = line.split(s);
        return new ArtShow(split[0],split[1],Integer.valueOf(split[2]),Integer.valueOf(split[3]));


    }
}




